#include "comm.hpp"

using namespace std;

// 管理管道文件
int main()
{
    Init init;

    // 打开信道
    int fd = open(FIFO_FILE, O_RDONLY); // 等待写入方打开之后，自己才会打开文件，向后执行, open 会阻塞
    if(fd < 0)
    {
        perror("open");
        exit(FIFO_OPEN_ERR);
    }

    cout << "server open file done" << endl;

    // 开始通信
    while(true)
    {
        char buffer[1024] = {0};
        int x = read(fd, buffer, sizeof(buffer));
        if(x > 0)
        {
            buffer[x] = 0;
            cout << "client say# " << buffer << endl;
        }
        else if(x == 0) 
        {
            cout << "client quit, me too!" << endl;
            break;
        }
        else break;
    }

    close(fd);

    return 0;
}